package com.lld.im.service.filter;


import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 将请求体中的数据缓存 实现多次请求体的多次读取
 */
public class CachingRequestBodyFilter extends OncePerRequestFilter {


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        ContentCachingRequestWrapper wrappedRequest =
                new ContentCachingRequestWrapper(request);
        wrappedRequest.getParameterMap();
        ServletInputStream inputStream = wrappedRequest.getInputStream();
//        byte[] contentAsByteArray = wrappedRequest.getContentAsByteArray();
        filterChain.doFilter(wrappedRequest, response);
    }
}
